package com.xc.content.controller;

import com.xc.content.model.dto.SaveTeachplanDto;
import com.xc.content.model.dto.TeachplanDto;
import com.xc.content.model.dto.TeachplanMediaDto;
import com.xc.content.service.TeachplanMediaService;
import com.xc.content.service.TeachplanService;

import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;

@RestController
@Slf4j
@AllArgsConstructor
@Api(tags = "课程计划相关接口")
public class TeachPlanController {

    private final TeachplanService teachplanService;

    private final TeachplanMediaService teachplanMediaService;

    @ApiOperation("查询课程计划树形结构")
    @ApiImplicitParam(value = "courseId", name = "课程Id", required = true, dataType = "Long", paramType = "path")
    @GetMapping("/teachplan/{courseId}/tree-nodes")
    public List<TeachplanDto> getTreeNodes(@PathVariable Long courseId) {
        log.info("查询课程计划树形结构:{}", courseId);
        return teachplanService.queryTeachPlanByCourseId(courseId);
    }

    @ApiOperation("新增或者修改章节、小节")
    @PostMapping("/teachplan")
    public void saveOrUpdateTeachplan(@RequestBody SaveTeachplanDto saveTeachplanDto) {
        log.info("新增或者修改章节、小节:{}", saveTeachplanDto);
        teachplanService.saveOrUpdateTeachplan(saveTeachplanDto);
    }

    @ApiOperation("删除章节")
    @DeleteMapping("/teachplan/{id}")
    public void delTeachplan(@PathVariable("id") Long id) {
        log.info("删除章节:{}", id);
        teachplanService.delTeachplan(id);
    }

    @ApiOperation("排序上移")
    @PostMapping("/teachplan/moveup/{id}")
    public void moveup(@PathVariable("id") Long id) {
        log.info("排序上移:{}", id);
        teachplanService.moveup(id);
    }

    @ApiOperation("排序下移")
    @PostMapping("/teachplan/movedown/{id}")
    public void movedown(@PathVariable("id") Long id) {
        log.info("排序上移:{}", id);
        teachplanService.movedown(id);
    }

    @ApiOperation("课程计划绑定媒体")
    @PostMapping("/teachplan/association/media")
    public void bindTeachplanMedia(@RequestBody TeachplanMediaDto dto) {
        log.info("课程计划绑定媒体:{}", dto);
        teachplanMediaService.bindTeachplanMedia(dto);
    }

    @ApiOperation("删除章节关联的媒体")
    @DeleteMapping("/teachplan/association/media/{teachplanId}/{mediaId}")
    public void deleteTeachplanMedia(@PathVariable("teachplanId") Long teachplanId, @PathVariable("mediaId") String mediaId) {
        log.info("删除章节关联的媒体:{},{}", teachplanId, mediaId);
        teachplanMediaService.deleteTeachplanMedia(teachplanId, mediaId);
    }
}
